<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<script>
  function insertSearch (find, arr, start, end) {
    start = start == null ? 0 : start
    end = end == null ? arr.length - 1 : end

    arr = quickSort(arr)

    if (start <= end && find >= arr[start] && find <= arr[end]) {
      if (arr[start] === find) return start
      if (arr[end] === find) return end

      // let mid = Math.ceil((start + end) / 2)
      let mid = start + Math.floor((find - arr[start]) / (arr[end] - arr[start]) * (end - start))

      if (arr[mid] === find) return mid

      if (find < arr[mid]) return insertSearch(find, arr, start, mid)

      if (find > arr[mid]) return insertSearch(find, arr, mid + 1, end)
    }

    return -1
  }

  function quickSort (arr) {
    if (arr.length <= 1) return arr

    const base = arr[0]
    arr = arr.slice(1)
    const left = []
    const right = []
    for (let i = 0; i < arr.length; i ++) {
      if (arr[i] <= base) {
        left.push(arr[i])
      } else {
        right.push(arr[i])
      }
    }
    return quickSort(left).concat(base).concat(quickSort(right))
  }

  console.log(insertSearch(9, [2, 4, 5, 1, 9]))
</script>
</body>
</html>
